package service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import vo.Producttype;
import vo.Saleorder;
import dao.ProductTypeDao;
import dao.ReportFormDao;
import dao.SaleDao;

/**
 * @author bohan
 * @create 2017年5月15日
 */
public class ReportFormServiceImpl implements ReportFormService{
	SaleDao saleDao;
	ReportFormDao dao;
	ProductTypeDao typeDao;
	
	public Map<String,Object> getMonthForm(String year){
		String hql = "from Saleorder where year(saleDateTime)='"+year+"' and paymentStatus='已支付'";
		List<Saleorder> list = new ArrayList<Saleorder>();
		List result = new ArrayList();
		Map<String,Object> map = new HashMap<String, Object>();
		list = saleDao.select(hql);
		for(int month = 0 ; month <= 11; month++){
			double sum = 0;
			for(int i = 0; i < list.size() ; i++){
				if(list.get(i).getSaleDateTime().getMonth()==month){
					sum = sum + Double.valueOf(list.get(i).getTotal());
				}
			}
			DecimalFormat df = new DecimalFormat("######0.00");  //格式化sum
			String sum2 = df.format(sum);
			result.add(sum2);
		}
		map.put("name", year);
		map.put("data", result);
		System.out.println(map.toString());
		return map;
	}


	public List getForm(String begin,String end){
		List list = new ArrayList();
		
		//获取所有商品类型
		List<Producttype> typelist = new ArrayList<Producttype>();
		typelist = typeDao.findAll();
		
		//获取销售记录，包括商品类型还有小计
		String sql = "SELECT tp. NAME,si.subTotal "
				+ "FROM producttype AS tp,product AS p,saleorder AS s,saleorderitem AS si "
				+ "WHERE tp.id = p.productType AND s.saleOrderNo = si.saleOrderNo "
				+ "AND s.paymentStatus='已支付'"
				+ "AND si.itemId = p.productNo "
				+ "AND s.saleDateTime BETWEEN '"+begin+"'AND '"+end+"';";
		List resultList = new ArrayList();
		resultList = dao.selectBySql(sql);
		
		//计算商品类型的销售额
		for(Producttype type : typelist){
			Map<String ,Object> map = new HashMap<String, Object>();
			double sum = 0;
			for(Object obj : resultList){
				JSONArray json = new JSONArray();
				json = JSONArray.parseArray(JSON.toJSONString(obj,SerializerFeature.DisableCircularReferenceDetect));
				if(type.getName().equals(json.get(0))){
					sum = sum + json.getDoubleValue(1);
				}
			}
			
			DecimalFormat df = new DecimalFormat("######0.00");  //格式化sum
			String sum2 = df.format(sum);
			map.put("name", type.getName());
			map.put("y", sum2);
			list.add(map);
		}
		System.out.println(list.toString());
		return list;
	}
	
	public SaleDao getSaleDao() {
		return saleDao;
	}

	public void setSaleDao(SaleDao saleDao) {
		this.saleDao = saleDao;
	}

	public ReportFormDao getDao() {
		return dao;
	}

	public void setDao(ReportFormDao dao) {
		this.dao = dao;
	}

	public ProductTypeDao getTypeDao() {
		return typeDao;
	}

	public void setTypeDao(ProductTypeDao typeDao) {
		this.typeDao = typeDao;
	}
	
}
